package com.huawei.hms.aaid.b;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hms.aaid.HmsInstanceId;
import com.huawei.hms.aaid.HmsInstanceIdEx;
import com.huawei.hms.aaid.c.b;
import com.huawei.hms.aaid.constant.AaidIdConstant.PUSH_CLIENT_SELF_INFO;
import com.huawei.hms.aaid.constant.a;
import com.huawei.hms.aaid.entity.TokenResp;
import com.huawei.hms.aaid.entity.TokenResult;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.common.internal.ResponseErrorCode;
import com.huawei.hms.common.internal.TaskApiCall;
import com.huawei.hms.support.api.client.Status;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.JsonUtil;
import o.fjx;

public class c extends TaskApiCall<d, TokenResult> {
    private Context a;

    public c(String str, String str2, Context context, String str3) {
        super(str, str2, str3);
        this.a = context;
    }

    protected void doExecute(d dVar, ResponseErrorCode responseErrorCode, String str, fjx<TokenResult> o_fjx_com_huawei_hms_aaid_entity_TokenResult) {
        if (responseErrorCode.getErrorCode() != 0) {
            HMSLog.e(HmsInstanceIdEx.TAG, "TokenTask failed, ErrorCode: " + responseErrorCode.getErrorCode());
            a a = a.a(responseErrorCode.getErrorCode());
            if (a != a.ERROR_UNKNOWN) {
                o_fjx_com_huawei_hms_aaid_entity_TokenResult.ॱ(a.a(a));
            } else {
                o_fjx_com_huawei_hms_aaid_entity_TokenResult.ॱ(new ApiException(new Status(responseErrorCode.getErrorCode(), responseErrorCode.getErrorReason())));
            }
        } else {
            TokenResp tokenResp = (TokenResp) JsonUtil.jsonToEntity(str, new TokenResp());
            a a2 = a.a(tokenResp.getRetCode());
            if (a2 != a.SUCCESS) {
                o_fjx_com_huawei_hms_aaid_entity_TokenResult.ॱ(a.a(a2));
                HMSLog.e(HmsInstanceIdEx.TAG, "TokenTask failed, StatusCode:" + a2.a());
            } else {
                Object tokenResult = new TokenResult();
                tokenResult.setToken(tokenResp.getToken());
                tokenResult.setBelongId(tokenResp.getBelongId());
                tokenResult.setRetCode(a.a(tokenResp.getRetCode()).a());
                o_fjx_com_huawei_hms_aaid_entity_TokenResult.ॱ(tokenResult);
                Object token = tokenResp.getToken();
                if (TextUtils.isEmpty(token)) {
                    HMSLog.i(HmsInstanceId.TAG, "GetTokenTask receive a empty token, please check HmsMessageService.onNewToken receive result.");
                    return;
                } else if (!com.huawei.hms.aaid.a.c.a(this.a, PUSH_CLIENT_SELF_INFO.FILE_NAME).equals(token)) {
                    HMSLog.i(HmsInstanceId.TAG, "receive a token, refresh the local token");
                    com.huawei.hms.aaid.a.c.a(this.a, PUSH_CLIENT_SELF_INFO.FILE_NAME, token);
                }
            }
        }
        b.a(dVar.getContext(), getUri(), responseErrorCode);
    }
}
